<?php

/**
 * This is the model class for table "zy_routing".
 *
 * The followings are the available columns in table 'zy_routing':
 * @property integer $routing_id
 * @property integer $user_id
 * @property string $IP
 * @property integer $tag
 * @property integer $Ispublic
 * @property string $domain
 * @property string $remark
 */
class Routing extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Routing the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_routing';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('IP, tag,maskbit', 'required'),
                        array('remark','safe'),
			array('user_id, tag, Ispublic,vpnid', 'numerical', 'integerOnly'=>true),
			array('IP', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('routing_id, user_id, IP, tag, Ispublic,remark,domain', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'routing_id' => 'Routing',
			'user_id' => Yii::t('base','UserName'),
			'IP' => Yii::t('base','IP'),
			'tag' => Yii::t('base','tag'),
			'Ispublic' =>Yii::t('base','Ispublic'),
			'maskbit'=>Yii::t('base','maskbit'),
                        'domain'=>Yii::t('base', 'Domain'),
                        'remark'=>Yii::t('base', 'Remark'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		if(yii::app()->user->level==1){
			$criteria=new CDbCriteria;
		}else{
			$user_id = yii::app()->user->id;
			$criteria=new CDbCriteria;
			$sql="SELECT id FROM zy_vpnlist WHERE id IN (SELECT b.field1 FROM zy_user_service a LEFT JOIN zy_login_list b ON a.`user_service_id`=b.user_service_id WHERE a.`user_id`=".$user_id." AND a.`package_id` IN (SELECT package_id FROM zy_package WHERE service_id IN
					(select service_id from zy_service where code = 'smartrouting')
					))";
			$vpnlist=Yii::app()->db->createCommand($sql)->queryAll();
                      //  var_dump($vpnlist);die();
			for($i=0;$i<count($vpnlist);$i++){
				$result[$i]=$vpnlist[$i]['id'];
			}
			//$criteria->addInCondition('vpnid',$result);
			
		}

		$criteria->compare('routing_id',$this->routing_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('IP',$this->IP,true);
		$criteria->compare('tag',$this->tag);
		$criteria->compare('Ispublic',$this->Ispublic);
		$criteria->compare('vpnid',$this->vpnid);
                $criteria->compare('domain',$this->domain);
                $criteria->compare('remark',$this->remark);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
				'sort'=>array(
						'defaultOrder'=>'routing_id DESC',
				),
		));
	}
	public function beforesave()
	{
		$id = yii::app()->user->id;
		if(yii::app()->user->level==1){
			$sql = "select IP from zy_routing where IP = '".$this->IP."' and Ispublic = '".$this->Ispublic."' AND routing_id != '".$this->routing_id."'";
		}else{
			$sql ="select IP from zy_routing where IP = '".$this->IP."' and user_id = '".$id."' AND routing_id !='".$this->routing_id."'";
		}
			$connection = Yii::app()->db;
			$command = $connection->createCommand($sql);
			$results = $command->queryAll();
			for($i=0;$i<count($results);$i++){
				$result[$i]=$results[$i]['IP'];
			}
			if($result!=""){
				// echo "<script>alert('The IP has setted!');</script>";
				$this->addError('IP', 'The IP has setted'); 
				return false;
			}else{
			return true;
		}

	}
	public function getUserName($id)
	{
		$rs = Routing::model()->findBypk($id);
		$rs2 = User::model()->find('user_id='.$rs->user_id);
		return $rs2->user_name;
	}
	public function getTagName($id)
	{
		$rs = Routing::model()->findBypk($id);
		if($rs->tag==1){
			$rs->tag  = Yii::t('base','Routing By VPN');
		}elseif($rs->tag==2){
			$rs->tag = Yii::t('base','Access Denied');
		}else{
			$rs->tag = Yii::t('base','Access By Local');
		}
		return $rs->tag;
	}
	public function checkVPN()
	{
		$id = yii::app()->user->id;
		$sql2 = "SELECT * FROM zy_service AS a LEFT JOIN zy_user_service AS b ON a.`service_id` = b.`service_id` WHERE( a.code='vpnsoftware' or a.code='smartrouting') And b.`user_id` =".$id;
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql2);
		$result2 = $command->queryAll();
		for($i=0;$i<count($result2);$i++){
			$rs[$i]=$result2[$i]['service_name'];
		}
		return $rs;
	}
}